<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */
class User extends MX_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('Admin_Layout');
        $this->load->model('user/user_model');
    }

    public function index() {
         $this->mycommon->authAdmin();
        $data = array();
        $data['headTitle'] = 'Quản lý người dùng';
        $data['current'] = 5;
        $this->load->library('Minifier');
        $data['user'] = $this->user_model->getAllUserInfo();
        $this->admin_layout->render('user/user_view', $data);
    }

    public function add() {
        $this->mycommon->authAdmin();
        $data = array();
        $data['error'] = '';
        $this->load->library('Form_validation');
        $this->load->library('Minifier');
        $this->load->library('PasswordHash');
        $this->minifier->AddJsNoPack('jquery.meio.mask');
        $this->load->model('user/user_model');
        $data['current'] = 5;
        $this->form_validation->CI = & $this;
        $this->form_validation->set_error_delimiters('<li>', '</li>');
        if ($this->input->post('submit') && $this->input->post('frmToken') && $this->session->userdata('frmToken') && $this->input->post('frmToken') == $this->session->userdata('frmToken')) {
            $this->form_validation->set_rules('user', 'user', 'required|min_length[5]|max_length[15]');
            $this->form_validation->set_rules('email', 'email', 'required|valid_email');
            $this->form_validation->set_rules('user', 'User', 'required|callback_user_check');
            $this->form_validation->set_rules('pass', 'Pass', 'required|min_length[6]|max_length[50]');
            $this->form_validation->set_rules('name', 'name', 'required|min_length[0]|max_length[50]');
//            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            if ($this->form_validation->run($this) == FALSE) {
                
            } else {
                $acc = $this->input->post("user", TRUE);
                $pwd = $this->input->post("pass", TRUE);
                $email = $this->input->post('email', TRUE);
                $name = $this->input->post('name', TRUE);
                $Pwd = $this->passwordhash->HashPassword($pwd);
//                $name = $this->input->post("name");
//                $data['name'] = $name;
                $this->user_model->add_user($acc, $Pwd, $email, $name);
                redirect(site_url('admin/user/index'));
            }
        }
        $data['frmToken'] = uniqid();
        $data['headTitle'] = 'Thêm người dùng';
        $this->session->set_userdata('frmToken', $data['frmToken']);
        $this->admin_layout->render('user/add_user_view', $data);
    }

    function edit($Id) {
        $this->mycommon->authAdmin();
        if ($Id == '' && intval($Id) != 0) {
            show_404();
        }
        $Id = intval($Id);
        $data = array();
        $data['error'] = '';
        $this->load->library('Form_validation');
        $this->load->library('Minifier');
        $this->load->library('PasswordHash');
        $this->minifier->AddJsNoPack('jquery.meio.mask');
        $this->load->model('user/user_model');
        $data['current'] = 5;
        $this->form_validation->CI = & $this;
        $this->form_validation->set_error_delimiters('<li>', '</li>');
        if ($this->input->post('submit') && $this->input->post('frmToken') && $this->session->userdata('frmToken') && $this->input->post('frmToken') == $this->session->userdata('frmToken')) {
            $this->form_validation->set_rules('user', 'user', 'required|min_length[5]|max_length[15]');
            $this->form_validation->set_rules('email', 'email', 'required|valid_email');
            $this->form_validation->set_rules('user', 'User', 'required|callback_user_check');
            $this->form_validation->set_rules('pass', 'Pass', 'required|min_length[6]|max_length[50]');
            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            if ($this->form_validation->run() == TRUE) {
                $user = $this->input->post("user");
                $pwd = $this->input->post("pass");
                $email = $this->input->post('email');
                $name = $this->input->post('name');
                
                $Pwd = $this->passwordhash->HashPassword($pwd);
                $array = array(
                    'acc'=>$user,
                    'pwd'=>$Pwd,
                    'email'=>$email,
                    'name'=>$name,
                );
                    if ($this->sim_model->edit_user($Id,$array)) {
                        redirect('admin/user/index');
            }
        }
        }
        $data['frmToken'] = uniqid();
        $data['headTitle'] = 'Sửa thông tin';
        $data['all'] = $this->user_model->getUserInfo($Id);
        $this->session->set_userdata('frmToken', $data['frmToken']);
        $this->admin_layout->render('user/edit_user_view', $data);
    }

    function delete($Id = '') {
        $this->mycommon->authAdmin();
        if ($Id == '' && intval($Id) != 0) {
            show_404();
        }
        $Id = intval($Id);
//        $this->load->model('setting/setting_model');
        $this->user_model->delete_user($Id);
        redirect('admin/user/index');
    }
    
     public function Banned($Id = '')
    {
        $this->mycommon->authAdmin();
        if ($Id == '') {
            show_404();
        }
        $data = array(
            'status' => '0'
        );
        $this->user_model->updateStatusUser($Id, $data);
        redirect('admin/user/index');
    }
    
    public function UnBanned($Id = '')
    {
        $this->mycommon->authAdmin();
        if ($Id == '') {
            show_404();
        }
        $data = array(
            'status' => '1'
        );
        $this->user_model->updateStatusUser($Id, $data);
        redirect('admin/user/index');
    }
}

?>
